package com.mymoney.ws.asynctasks;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.mymoney.MyMoneyActivity;
import com.mymoney.constants.RequestPaths;
import com.mymoney.data.access.JsonParser;
import com.mymoney.exceptions.CommunicationException;
import com.mymoney.ws.client.SSLHttpClient;
import com.mymoney.ws.data.ItemBean;

/**
 * AsyncTask that call RegisterDevice web service
 *
 * @author dimitar.zlatkov
 */
public abstract class GetPlannedItemsAsyncTask extends AsyncTask<String, Void, Void> {

    private static String TAG = "GetPlannedItemsAsyncTask";
    private Context context;
    private ItemBean[] itemList;


	public GetPlannedItemsAsyncTask(MyMoneyActivity context) {
        this.context = context;
    }

    @Override
    protected Void doInBackground(String... arg0) 
    {
        Log.i(TAG, "GetPlannedItemsAsyncTask on background start...");
        SSLHttpClient httpClient = new SSLHttpClient(context);

        String path = RequestPaths.getInstance().createGetPlannedItemsPath();
        try {
            String responceBody = httpClient.get(path);
            setListOfItems(JsonParser.getInstance().getItemsResponce(responceBody));

        }
        catch (CommunicationException e) {
        	Log.e(TAG, "Immediate internal error", e);
        }
        catch (Exception e) {
            Log.e(TAG, "Immediate internal error", e);
        }
            
            return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        onFinish();
    }


    public abstract void onFinish();

	public ItemBean[] getListOfItems() {
		return itemList;
	}

	private void setListOfItems(ItemBean[] deviceResponseBean) {
		this.itemList = deviceResponseBean;
	}
}
